package cn.tedu.tmall.admin.mall.dao.repository.impl;

import cn.tedu.tmall.admin.mall.dao.mapper.MallCategoryMapper;
import cn.tedu.tmall.admin.mall.dao.mapper.MallCategoryTreeMapper;
import cn.tedu.tmall.admin.mall.dao.repository.IMallCategoryRepository;
import cn.tedu.tmall.admin.mall.pojo.po.MallCategoryPO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 数据操作层-商城分类接口实现类
 */
@Repository
public class MallCategoryRepository implements IMallCategoryRepository {
    @Autowired
    private MallCategoryMapper mallCategoryMapper;
    @Autowired
    private MallCategoryTreeMapper mallCategoryTreeMapper;

    @Override
    public Integer countCategoryByName(String name) {
        QueryWrapper<MallCategoryPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name);
        return mallCategoryMapper.selectCount(queryWrapper);
    }

    @Override
    public MallCategoryPO getParentCategoryById(Long parentId) {
        return mallCategoryMapper.selectById(parentId);
    }

    @Override
    public Integer save(MallCategoryPO mallCategoryPO) {
        return mallCategoryMapper.insert(mallCategoryPO);
    }

    @Override
    public void updateCategoryById(MallCategoryPO updateCategory) {
        mallCategoryMapper.updateById(updateCategory);
    }

    @Override
    public List<MallCategoryPO> getAll() {
        QueryWrapper<MallCategoryPO> queryWrapper = new QueryWrapper<>();
        return mallCategoryMapper.selectList(queryWrapper);
    }

    //    @Override
    public List<MallCategoryPO> getChildrenCategoryByParentId(Long parentId) {
        QueryWrapper<MallCategoryPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",parentId);
        return mallCategoryMapper.selectList(queryWrapper);
    }

    @Override
    public List<MallCategoryPO> getTopCategory() {
        return getChildrenCategoryByParentId(0L);
    }

    @Override
    public List<MallCategoryPO> getChildrenCategory(Long parentId) {
        return getChildrenCategoryByParentId(parentId);
    }

    @Override
    public int countCategoryById(Long categoryId) {
        QueryWrapper<MallCategoryPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",categoryId);
        return mallCategoryMapper.selectCount(queryWrapper);
    }


}
